import sys, uuid, os, time, datetime, re
from math import *
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import pylab 
from matplotlib.ticker import ScalarFormatter
from matplotlib import rc
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
from matplotlib.cbook import iterable
import numpy as np

WEB2PY_ROOT = os.getenv('WEB2PY_ROOT')

def GCtoMDP(datafile):
    import uuid
    tmpfile = 'tmp.%s' % uuid.uuid4()
    command='python '+os.path.join(WEB2PY_ROOT,request.folder,'private/development/Converters/universal_converter.py')
    command=command+ ' '+datafile +'> %s' %tmpfile
    print command
    os.system(command)    
    match = re.search('PLAQUETTE = (?P<p>\d\.\d+)', open(tmpfile,'rb').read())
    os.unlink(tmpfile)
    if match: return float(match.group('p'))

def Plaquette(datafile): # converted file
    import uuid
    tmpfile = 'tmp.%s' % uuid.uuid4()
    command=os.path.join(WEB2PY_ROOT,request.folder,'private/development/Libraries/make_plaquette.exe')
    command=command+ ' '+datafile+'>' %tmpfile
    os.system(command)    
    match = re.compile('PLAQUETTE = (?P<p>\d.\d+)').find(open(tmpfile,'rb').read())    
    os.unlink(tmpfile)
    if match: return float(match.group('p'))
    
def MDPtoVTK_topological_charge(datafile):
    command=os.path.join(WEB2PY_ROOT,request.folder,'private/development/Libraries/cool_and_topological.exe')
    command=command+' '+datafile
    os.system(command)

def MDPtoVTK_polyakov(datafile):
    command=os.path.join(WEB2PY_ROOT,request.folder,'private/development/Libraries/polyakov.exe')
    command=command+' '+datafile
    os.system(command)

def MAplot():
    streams = db(db.stream.id>0).select()
    for stream in streams:
        configs = db(db.configfile.stream==stream.id).select()
        malist=[]
        plaquette=[]
        maplaquette=[]
        x=[]
        for config in configs:
            if config.plaquette!= None and config.plaquette!='<NULL>':
                malist.append((config.filename,config.plaquette))
        malist=sorted(malist)
        if malist!=[]:
            maplaquette.append(malist[0][1])
            plaquette.append(malist[0][1])
        else:
            maplaquette.append(0)
            plaquette.append(0)
        for i in range(1,len(malist)):
            maplaquette.append((maplaquette[i-1]*i+malist[i][1])/(i+1))
            plaquette.append(malist[i][1])
        for i in range(len(maplaquette)):
            x.append(i)
        fig=plt.figure()
        ax = fig.add_subplot(111)#.yaxis
        #ax=fig.yaxis
        plt.plot(x,maplaquette)
        plt.title('Plaquette Moving Average')
        #ax.yaxis.major.formatter.set_scientific(False)
        #ax.tick.ScalarFormatter(useOffset=False)
        ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))
        plt.savefig('maplotfig',format='png')
        maplotfile = os.path.join(WEB2PY_ROOT, 'maplotfig')
        stream.update_record(maplot=db.stream.maplot.store(open(maplotfile,'rb'),".png"))
        db.commit()
        os.unlink(maplotfile)
        fig2=plt.figure()
        ax = fig2.add_subplot(111)
        plt.plot(x,plaquette)
        plt.title('Plaquette')
        ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))
        plt.savefig('plplotfig',format='png')
        plplotfile = os.path.join(WEB2PY_ROOT, 'plplotfig')
        stream.update_record(plplot=db.stream.plplot.store(open(plplotfile,'rb'),".png"))
        db.commit()
        os.unlink(plplotfile)
        
            
def process(id=None):
    if id is None:
        raise Exception('id is required')
    row = db(db.processedfile.id == int(id)).select().first()
    if row is None:
        raise Exception('no row found with id %s.' % id)
    print 'processing row id %s.' % id
    row.update_record(status = 'processing')
    db.commit()
    configfile = row.configfile
    try:
        if not configfile.converted_file:
            if not configfile.file.endswith('.mdp'):
                GCtoMDP(os.path.join(request.folder,'uploads',configfile.file))
                print sys.path[0]
                mdpfile = os.path.join(WEB2PY_ROOT, row.configfile.file + ".mdp")
                mdpfile = db.configfile.converted_file.store(open(mdpfile,'rb'),".mdp")
                configfile.update_record(converted_file=mdpfile)
                os.unlink(mdpfile)
                MAplot()
            else:
                mdpfile = configfile.file                
                configfile.update_record(converted_file=mdpfile)
            db.commit()
        else:
            mdpfile = configfile.converted_file

        if not configfile.plaquette:
            configfile.update_record(plaquette=Plaquette(mdpfile))
            db.commit()

        print ('about to convert %s\n' % mdpfile)
        if row.algorithm == 'topological_charge':
            MDPtoVTK_topological_charge(os.path.join(request.folder,'uploads',mdpfile))
            print ('done\n')
            vtkfile = mdpfile + ".topological_charge_20.vtk"
            vtkfile2 = mdpfile + ".cooled20"
            vtkfile3 = mdpfile + ".topological_charge_20.sum.vtk"
            vtkfile4 = mdpfile + ".topological_charge_20.flat.vtk"
            row.update_record(vtkfile=db.processedfile.vtkfile.store(open(os.path.join(request.folder,'uploads',vtkfile),'rb'),".vtk")\
                                  , status=COMPLETED,completed_processing_on=datetime.datetime.today())
            os.unlink(os.path.join(request.folder,'uploads',vtkfile))
            os.unlink(os.path.join(request.folder,'uploads',vtkfile2))
            os.unlink(os.path.join(request.folder,'uploads',vtkfile3))
            os.unlink(os.path.join(request.folder,'uploads',vtkfile4))
        elif row.algorithm == 'polyakov':
            MDPtoVTK_polyakov(os.path.join(request.folder,'uploads',mdpfile))
            print('done\n')
            vtkfile_real = mdpfile + ".real_polyakov_line.vtk"
            vtkfile_imag = mdpfile + ".imag_polyakov_line.vtk"
            row.update_record(vtkfile=db.processedfile.vtkfile.store(open(os.path.join(request.folder,'uploads',vtkfile_real),'rb'),".vtk")\
                                  , status=COMPLETED,completed_processing_on=datetime.datetime.today())
            os.unlink(os.path.join(request.folder,'uploads',vtkfile_real))
            os.unlink(os.path.join(request.folder,'uploads',vtkfile_imag))
    except Exception,e:
        print (str(e)+'\n\n')
        row.update_record(status = 'failed')
    db.commit() 

id = int(sys.argv[-1])
process(id)

